package com.cyh;

import org.junit.Test;

/**
 * Created by yanhuche on 9/27/2016.
 */
public class EqualSign__Equals {


    @Test
    public void test01() {
        String a = new String("one");
        String b = new String("one");
        System.out.println(a == b); // memory address, different instance leading to different address
        System.out.println(a.equals(b)); // value
    }

    @Test
    public void test02() {
        String a = "one";
        String b = "one";
        System.out.println(a == b); // memory address, constant pool, same address
        System.out.println(a.equals(b)); // value
    }

    @Test
    public void test03() {
        String a = new String("one");
        String b = "one";
        System.out.println(a == b); // memory address, one is instance, one is constant pool 
        System.out.println(a.equals(b)); // value
    }

    @Test
    public void test04() {
        String a = new String("one");
        String b = new String(a);
        System.out.println(a == b); // memory address, different instance leading to different address 
        System.out.println(a.equals(b)); // value
    }

    @Test
    public void test05() {
        String a = "one";
        String b = new String(a);
        System.out.println(a == b); // memory address, one is constant pool, one is instance 
        System.out.println(a.equals(b)); // value
    }

    @Test
    public void test06() {
        Student a = new Student("one", 7);
        Student b = new Student("one", 7);
        System.out.println(a == b);
        System.out.println(a.equals(b));
    }



    class Student {
        String name;
        int age;

        public Student(String name, int age) {
            this.name = name;
            this.age = age;
        }
    }



}
/*

简单的说：==比较的是地址，而equals比较的是内容


1）对于==，如果作用于基本数据类型的变量，则直接比较其存储的“值”是否相等；
    如果作用于引用类型的变量，则比较的是所指向的对象的地址

2）对于equals方法，注意equals方法不能作用于基本数据类型的变量
    如果没有对equals方法进行重写，则比较的是引用类型的变量所指向的对象的地址；
    诸如String、Date等类对equals方法进行了重写的话，比较的是所指向的对象的内容。

*/
